﻿using IDAL;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;

namespace DAL
{
    public class PatientDAL : IPatientDAL
    {
        /// <summary>
        /// 新增患者信息
        /// </summary>
        /// <returns></returns>
        public int AddPatient(Patient model)
        {
            using (MyDBContext db = new MyDBContext())
            {
                var datetime = DateTime.Now.ToString("yyyyMMddHHmmss");
                model.Patient_Num = "HZ" + datetime;
                model.Patient_Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                db.Patient.Add(model);
                return db.SaveChanges();
            }

        }
        /// <summary>
        /// 查询科室信息
        /// </summary>
        /// <returns></returns>
        public List<Outpatient> GetOutpatient()
        {
            using (MyDBContext db = new MyDBContext())
            {
                return db.Outpatient.ToList();
            }

        }

        /// <summary>
        /// 查询患者信息
        /// </summary>
        /// <returns>返回患者信息</returns>
        public List<PatientTwo> GetPatient(string str)
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = db.Patient.Join(db.Outpatient, a => a.Outpatient_Id, b => b.Outpatient_Id, (a, b) => new PatientTwo
                {
                    Patient_Id = a.Patient_Id,
                    Patient_Num = a.Patient_Num,
                    Patient_Name = a.Patient_Name,
                    Patient_Sex = a.Patient_Sex,
                    Patient_Age = a.Patient_Age,
                    Outpatient_Id = b.Outpatient_Id,
                    Outpatient_Name = b.Outpatient_Name,
                    Patient_IPhone = a.Patient_IPhone,
                    Patient_Date = a.Patient_Date,
                    Patient_State = a.Patient_State
                });

                if (!string.IsNullOrEmpty(str))
                {
                    list = list.Where(e => e.Patient_Name.Contains(str));
                }

                list = list.OrderByDescending(e => e.Patient_Date);

                return list.ToList();
            }
        }
        /// <summary>
        /// 根据id查询单条数据（编辑详情）
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        public Patient GetPatientById(int pid)
         {
            using (MyDBContext db = new MyDBContext())
            {
                var list= db.Patient.Where(e => e.Patient_Id == pid).FirstOrDefault();
                return list;
            }
        }
        /// <summary>
        /// 修改就诊状态
        /// </summary>
        /// <param name="pid"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        public int UpdatePatientById(int pid, int state)
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = db.Patient.Where(e => e.Patient_Id == pid).FirstOrDefault();
                list.Patient_State = state;

                return db.SaveChanges();
            }
        }
        /// <summary>
        /// 修改患者信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdatePatient(Patient model)
        {
            using (MyDBContext db = new MyDBContext())
            {
                var list = db.Patient.Where(e => e.Patient_Id == model.Patient_Id).FirstOrDefault();
                list.Patient_Name = model.Patient_Name;
                list.Patient_IDnumber = model.Patient_IDnumber;
                list.Patient_Age = model.Patient_Age;
                list.Patient_Sex = model.Patient_Sex;
                list.Patient_Birthday = model.Patient_Birthday;
                list.Patient_IPhone = model.Patient_IPhone;
                list.Patient_Email = model.Patient_Email;
                list.Outpatient_Id = model.Outpatient_Id;
                list.Patient_Home = model.Patient_Home;
                return db.SaveChanges();
            }
        }
    }
}
